package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.ProcUser;
import cn.getech.data.development.mapper.ProcUserMapper;
import cn.getech.data.development.service.ProcUserService;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.PojoUtils;
import cn.getech.data.intelligence.common.utils.Query;
import cn.getech.system.center.entity.SysUserEntity;
import cn.getech.system.center.mapper.SysUserMapper;
import cn.getech.system.center.model.dto.SysUserDto;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 项目用户关系表 服务实现类
 * </p>
 *
 * @author wcj
 * @since 2019-10-29
 */
@Service
public class ProcUserServiceImpl extends ServiceImpl<ProcUserMapper, ProcUser> implements ProcUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PageUtils procUserList(Map<String, Object> params) {
        IPage<Map<String, Object>> page = this.baseMapper.procUserList((Page<Map<String, Object>>) new Query<Map<String, Object>>().getPage(params), params);
        return new PageUtils(page);
    }
    @Override
    public List<Integer> procUserIdList(Integer procId) {
        return this.baseMapper.procUserIdList(procId);
    }

    @Override
    public List<Integer> procIdListByUserId(Integer userId) {
        Integer tenantId = ShiroUtils.getTenantId();
        return this.baseMapper.procIdListByUserId(userId,tenantId);
    }

    @Override
    public List<SysUserDto> listProcRelUsers(Integer procId) {
        //查询到关联的用户
        List<ProcUser> procUsers = baseMapper.selectList(new QueryWrapper<ProcUser>().eq("proc_id", procId));
        if(CollectionUtil.isNotEmpty(procUsers)){
            Set<Integer> listUsers = new HashSet<>();
            for (ProcUser procUser : procUsers) {
                listUsers.add(procUser.getUserId());
            }
            List<SysUserEntity> lists = sysUserMapper.selectListByIds(listUsers);
            return PojoUtils.listConvert(SysUserDto.class,lists);
        }
        return null;
    }

    @Override
    public boolean checkExistByProcUser(ProcUser procUser) {
        QueryWrapper queryWrapper=new QueryWrapper<ProcUser>();
        if (null!=procUser.getId()){
            queryWrapper.eq("id", procUser.getId());
        }
        if(null!=procUser.getProcId()) {
            queryWrapper.eq("proc_id",procUser.getProcId());
        }
        if(null!=procUser.getUserId()){
            queryWrapper.eq("user_id",procUser.getUserId());
        }
        return this.baseMapper.selectCount(queryWrapper)>0;
    }
    @Override
    public boolean checkViewPermission(Integer procId) {
        if(procId == null){
            return false;
        }
        List<ProcUser> list= this.baseMapper.selectList(new QueryWrapper<ProcUser>().eq("proc_id",procId));
        if(CollectionUtil.isNotEmpty(list)){
            for (ProcUser procUser:list
            ) {
                if(procUser.getUserId()== ShiroUtils.getUserId().intValue()){
                    return  true;
                }
            }
        }
        return false;
    }

    @Override
    public void deleteByUserId(Long userId) {
        this.baseMapper.delete(new QueryWrapper<ProcUser>().eq("user_id",userId));
    }
}
